<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="1. 访问SDRAM存储器​    在stm32上初始化SDRAM之后，就可用它来存储数据。SDRAM的存储空间是被映射到内核的寻址区域的，可以通过映射的地址直接访问 SDRAM。访问这些地址时，FMC 外设自动读写 SDRAM，程序上无需额外操作。 1.1 使用指针访问​    通过地址访问内存，最直接的方式就是使用 C 语言的指针，如下图代码所示： 123#define SDRAM_BANK_">
<meta property="og:type" content="article">
<meta property="og:title" content="分散加载文件分配变量到外部SDRAM">
<meta property="og:url" content="http://example.com/2021/03/04/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%86%E9%85%8D%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SDRAM/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="1. 访问SDRAM存储器​    在stm32上初始化SDRAM之后，就可用它来存储数据。SDRAM的存储空间是被映射到内核的寻址区域的，可以通过映射的地址直接访问 SDRAM。访问这些地址时，FMC 外设自动读写 SDRAM，程序上无需额外操作。 1.1 使用指针访问​    通过地址访问内存，最直接的方式就是使用 C 语言的指针，如下图代码所示： 123#define SDRAM_BANK_">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210225203633.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210225204134.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210225205850.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302211535.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302212648.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302213955.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302214356.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210303212905.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210304093015.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210304094021.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210304094531.png">
<meta property="article:published_time" content="2021-03-04T02:27:30.000Z">
<meta property="article:modified_time" content="2021-03-04T02:27:30.000Z">
<meta property="article:author" content="xudonglei">
<meta property="article:tag" content="分散加载文件">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210225203633.png">

<link rel="canonical" href="http://example.com/2021/03/04/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%86%E9%85%8D%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SDRAM/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>分散加载文件分配变量到外部SDRAM | 学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Do not bb, show me the example</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/04/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%86%E9%85%8D%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SDRAM/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          分散加载文件分配变量到外部SDRAM
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-04 10:27:30" itemprop="dateCreated datePublished" datetime="2021-03-04T10:27:30+08:00">2021-03-04</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/03/04/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%86%E9%85%8D%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SDRAM/" class="post-meta-item leancloud_visitors" data-flag-title="分散加载文件分配变量到外部SDRAM" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/04/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%86%E9%85%8D%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SDRAM/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/04/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%86%E9%85%8D%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SDRAM/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="1-访问SDRAM存储器"><a href="#1-访问SDRAM存储器" class="headerlink" title="1. 访问SDRAM存储器"></a>1. 访问SDRAM存储器</h2><p>​    在stm32上初始化SDRAM之后，就可用它来存储数据。SDRAM的存储空间是被映射到内核的寻址区域的，可以通过映射的地址直接访问 SDRAM。访问这些地址时，FMC 外设自动读写 SDRAM，程序上无需额外操作。</p>
<h3 id="1-1-使用指针访问"><a href="#1-1-使用指针访问" class="headerlink" title="1.1 使用指针访问"></a>1.1 使用指针访问</h3><p>​    通过地址访问内存，最直接的方式就是使用 C 语言的指针，如下图代码所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SDRAM_BANK_ADDR ((uint32_t)0X68000000)</span></span><br><span class="line"><span class="comment">/*写16位数据*/</span></span><br><span class="line">*( <span class="keyword">uint16_t</span>*) (SDRAM_BANK_ADDR) = (<span class="keyword">uint16_t</span>)<span class="number">0x1234</span>;</span><br></pre></td></tr></table></figure>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210225203633.png" style="zoom:67%;">

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*写32位数据*/</span></span><br><span class="line">*( <span class="keyword">uint32_t</span>*) (SDRAM_BANK_ADDR) = (<span class="keyword">uint32_t</span>)<span class="number">0x12345678</span>;</span><br></pre></td></tr></table></figure>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210225204134.png" style="zoom:67%;">

<h3 id="1-2-直接指定变量存储到SDRAM空间"><a href="#1-2-直接指定变量存储到SDRAM空间" class="headerlink" title="1.2 直接指定变量存储到SDRAM空间"></a>1.2 直接指定变量存储到SDRAM空间</h3><p>​    每次存取数据都使用指针来访问太麻烦了，为了简化操作，可以直接指定变量存储到SDRAM 空间。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*绝对定位方式访问 SDRAM,这种方式必须定义成全局变量*/</span></span><br><span class="line"><span class="keyword">uint16_t</span> testValue __attribute__((at(SDRAM_BANK_ADDR)));</span><br><span class="line">testValue = <span class="number">0x5678</span>;</span><br></pre></td></tr></table></figure>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210225205850.png" style="zoom:67%;">

<p>​    这种方式使用关键字“<strong>attribute</strong>((at()))”来指定变量的地址，代码中指定 testValue存储到SDRAM的起始地址，从而实现把变量存储到SDRAM上。更常见的是利用这种方法定义一个很大的数组，整个数组都指定到SDRAM地址上，然后就像使用malloc 函数一样，用户自定义一些内存管理函数，动态地使用 SDRAM 的内存。</p>
<h2 id="2-修改分散加载文件分配变量到外部SDRAM空间"><a href="#2-修改分散加载文件分配变量到外部SDRAM空间" class="headerlink" title="2. 修改分散加载文件分配变量到外部SDRAM空间"></a>2. 修改分散加载文件分配变量到外部SDRAM空间</h2><p>​    这边的实验以及一些资料都来源于野火，在这表示感谢。上面我们使用指针或者<strong>attribute</strong>((at(具体地址)))来指定变量的位置，当有多个这样的变量时，就需要手动计算地址空间了，非常麻烦。所以我们将修改 sct 文件，让链接器自动分配全局变量到 SDRAM 的地址并进行管理，使得利用 SDRAM 的空间就跟内部 SRAM 一样简单。</p>
<h3 id="2-1-修改启动文件，在-main-执行之前初始化-SDRAM"><a href="#2-1-修改启动文件，在-main-执行之前初始化-SDRAM" class="headerlink" title="2.1 修改启动文件，在__main 执行之前初始化 SDRAM"></a>2.1 修改启动文件，在__main 执行之前初始化 SDRAM</h3><p>​    芯片启动后，会通过<strong>_main 函数调用分散加载代码 _scatterload</strong>，分散加载代码会把存储在 FLASH 中的 RW-data 复制到 RAM中，然后在 RAM 区开辟一块 ZI-data 的空间，并将其初始化为 0 值。因此，为了保证在程</p>
<p>序中定义到 SDRAM 中的变量能被正常初始化，我们需要在系统执行分散加载代码之前使SDRAM 存储器正常运转，使它能够正常保存数据。</p>
<p>​    我们以前会使用 FSMC_SRAM_Init函数初始化 SDRAM，且该函数在 main 函数里才被调用，所以在 SDRAM 正常运转之前，分散加载过程复制到SDRAM 中的数据都丢失了。所以现在我们需要在跳转到 main 函数之前就被调用，具体见 startup_stm32f40_41xxx.s文件。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302211535.png" style="zoom:67%;">

<p>​    在执行__main 函数前先调用了我们自定义的FSMC_SRAM_Init函数，从而为分散加载代码准备好正常的硬件工作环境。</p>
<h3 id="2-2-sct-文件增加外部SDRAM空间对应的执行域"><a href="#2-2-sct-文件增加外部SDRAM空间对应的执行域" class="headerlink" title="2.2 sct 文件增加外部SDRAM空间对应的执行域"></a>2.2 sct 文件增加外部SDRAM空间对应的执行域</h3><p>​    修改sct文件，控制使得在C源文件中定义的全局变量都自动由链接器分配到外部SDRAM中。把“Options for Target-&gt;Linker-&gt;Use Memory Layout from Target Dialog”中的勾去掉。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302212648.png" style="zoom:50%;">

<p>​    sct文件的格式如下所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302213955.png" style="zoom:50%;">

<p>​    keil中自动生成的sct文件如下图所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210302214356.png" style="zoom:50%;">

<p>​    上图<strong>执行域中</strong>包含了ER_IROM1及RW_IRAM1两个执行域，它们分别对应描述了STM32的内部 FLASH 及内部 SRAM 的基地址及空间大小。而它们内部的“输入节区描述”说明了哪些节区要存储到这些空间，链接器会根据它来处理编排这些节区。</p>
<p>​    修改之后的sct文件如下图所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210303212905.png" style="zoom:60%;">

<ul>
<li>RW_ERAM1是我们配置的 SDRAM 执行域。该执行域的名字是可以随便取的，最重要的是它的基地址及空间大小，这两个值与我们实验板配置的 SDRAM 基地址及空间大小一致，所以该执行域会被映射到 SDRAM 的空间。在 RW_ERAM1 执行域内部，它使用“.ANY(+RW +ZI)”语句，选择了所有的 RW/ZI 类型的数据都分配到这个SDRAM 区域，所以我们在工程中的 C 文件定义全局变量时，它都会被分配到这个SDRAM 区域。</li>
<li>RW_IRAM1是STM32内部SRAM的执行域。我们在默认配置中增加了“*.o(STACK)语句。本来上面配置 SDRAM 执行域后已经达到使全局变量分配的目的，为何还要修改原内部 SRAM 的执行域呢？这是由于我们在__main 之前调用的 SystemInit 函数和 FSMC_SRAM_Init函数内部定义了一些局部变量，而函数内的局部变量是需要分配到“栈”空间(STACK)。所以在 FSMC_SRAM_Init函数执行之前，栈空间必须要被准备好，然而在 FSMC_SRAM_Init函数执行之前，SDRAM 芯片却并未正常工作，这样的矛盾导致栈空间不能被分配到 SDRAM。</li>
<li>虽然内部 SRAM 的执行域 RW_IRAM1 及 SDRAM 执行域 RW_ERAM1 中都使用“.ANY(+RW +ZI)”语句选择了所有 RW 及 ZI 属性的内容，但对于符合两个相同选择语句的内容，链接器会优先选择使用空间较大的执行域，即这种情况下只有当SDRAM 执行域的空间使用完了，RW/ZI 属性的内容才会被分配到内部 SRAM。</li>
<li>所以在大部分情况下，<strong>内部 SRAM 执行域中的“.ANY(+RW +ZI)”语句是不起作用的()，而栈节区(STACK)又属于 ZI-data 类，如果我们的内部 SRAM 执行域还是按原来的默认配置的话，栈节区会被分配到外部 SDRAM，导致出错</strong>。为了避免这个问题，我们把栈节区使用“*.o(STACK)”语句分配到内部 SRAM 的执行域。</li>
</ul>
<h2 id="3-变量分配测试"><a href="#3-变量分配测试" class="headerlink" title="3. 变量分配测试"></a>3. 变量分配测试</h2><p>​    这边的测试代码直接参考的野火的，如下所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义变量到 SDRAM</span></span><br><span class="line"><span class="keyword">uint32_t</span> testValue =<span class="number">7</span> ;</span><br><span class="line"><span class="comment">//定义变量到 SDRAM</span></span><br><span class="line"><span class="keyword">uint32_t</span> testValue2 =<span class="number">0</span>;</span><br><span class="line"><span class="comment">//定义数组到 SDRAM</span></span><br><span class="line"><span class="keyword">uint8_t</span> testGrup[<span class="number">100</span>] =&#123;<span class="number">0</span>&#125;;</span><br><span class="line"><span class="comment">//定义数组到 SDRAM</span></span><br><span class="line"><span class="keyword">uint8_t</span> testGrup2[<span class="number">100</span>] =&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123; </span><br><span class="line">	<span class="keyword">uint32_t</span> inerTestValue =<span class="number">10</span>;</span><br><span class="line">	delay_init(<span class="number">168</span>);		       </span><br><span class="line">	uart_init(<span class="number">115200</span>);</span><br><span class="line">  </span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;inerTestValue,inerTestValue);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testValue,testValue);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testValue2,testValue2);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d,%d,%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testGrup,testGrup[<span class="number">0</span>],testGrup[<span class="number">1</span>],testGrup[<span class="number">2</span>]);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;地址：0x%x,  值为：%d，%d,%d\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)&amp;testGrup2,testGrup2[<span class="number">0</span>],testGrup2[<span class="number">1</span>],testGrup2[<span class="number">2</span>]);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">uint32_t</span> * pointer = (<span class="keyword">uint32_t</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">uint32_t</span>)*<span class="number">3</span>);</span><br><span class="line">	<span class="keyword">if</span>(pointer != <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		*(pointer)=<span class="number">1</span>;</span><br><span class="line">        *(++pointer)=<span class="number">2</span>;</span><br><span class="line">        *(++pointer)=<span class="number">3</span>; </span><br><span class="line"></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;地址为：0x%x\r\n&quot;</span>,(<span class="keyword">uint32_t</span>)pointer); </span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;*(pointer--)=%d, \r\n&quot;</span>,*(pointer--));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;*(pointer--)=%d, \r\n&quot;</span>,*(pointer--));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;*(pointer)=%d, \r\n&quot;</span>,*(pointer));</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		GPIO_SetBits(GPIOF , GPIO_Pin_9);</span><br><span class="line">		delay_ms(<span class="number">1000</span>);</span><br><span class="line">		GPIO_ResetBits(GPIOF , GPIO_Pin_9);</span><br><span class="line">		delay_ms(<span class="number">1000</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>​    运行结果如下图：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210304093015.png" style="zoom:67%;">

<p>​    并查看工程的 map 文件观察变量的分配情况：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210304094021.png" style="zoom:80%;">

<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210304094531.png"></p>
<p>​    从 map 文件中，可看到 startup_stm32f40_41xxx.s 的 RW-data 及栈空间节区(STACK)都被分配到了 RW_IRAM1 区域，即 STM32 的内部 SRAM 空间中；而 main 文件中定义的 RW-data、ZI-data 以及堆空间节区(HEAP)都被分配到了 RW_ERAM1 区域，即我们扩展的SDRAM 空间中，看起来一切都与我们的 sct 文件配置一致了。(堆空间属于 ZI-data，由于没有像控制栈节区那样指定到内部 SRAM，所以它被默认分配到 SDRAM 空间了)</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6/" rel="tag"># 分散加载文件</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/02/28/ARM%E4%B8%AD%E7%9A%84%E5%8A%A0%E8%BD%BD%E5%9F%9F%E5%92%8C%E8%BF%90%E8%A1%8C%E5%9F%9F/" rel="prev" title="ARM中加载域和运行域">
      <i class="fa fa-chevron-left"></i> ARM中加载域和运行域
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/03/07/esp32%E5%B0%81%E8%A3%85lib%E5%BA%93/" rel="next" title="esp32封装lib库">
      esp32封装lib库 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%AE%BF%E9%97%AESDRAM%E5%AD%98%E5%82%A8%E5%99%A8"><span class="nav-number">1.</span> <span class="nav-text">1. 访问SDRAM存储器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BD%BF%E7%94%A8%E6%8C%87%E9%92%88%E8%AE%BF%E9%97%AE"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 使用指针访问</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E7%9B%B4%E6%8E%A5%E6%8C%87%E5%AE%9A%E5%8F%98%E9%87%8F%E5%AD%98%E5%82%A8%E5%88%B0SDRAM%E7%A9%BA%E9%97%B4"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 直接指定变量存储到SDRAM空间</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BF%AE%E6%94%B9%E5%88%86%E6%95%A3%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%86%E9%85%8D%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SDRAM%E7%A9%BA%E9%97%B4"><span class="nav-number">2.</span> <span class="nav-text">2. 修改分散加载文件分配变量到外部SDRAM空间</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%BF%AE%E6%94%B9%E5%90%AF%E5%8A%A8%E6%96%87%E4%BB%B6%EF%BC%8C%E5%9C%A8-main-%E6%89%A7%E8%A1%8C%E4%B9%8B%E5%89%8D%E5%88%9D%E5%A7%8B%E5%8C%96-SDRAM"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 修改启动文件，在__main 执行之前初始化 SDRAM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-sct-%E6%96%87%E4%BB%B6%E5%A2%9E%E5%8A%A0%E5%A4%96%E9%83%A8SDRAM%E7%A9%BA%E9%97%B4%E5%AF%B9%E5%BA%94%E7%9A%84%E6%89%A7%E8%A1%8C%E5%9F%9F"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 sct 文件增加外部SDRAM空间对应的执行域</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%8F%98%E9%87%8F%E5%88%86%E9%85%8D%E6%B5%8B%E8%AF%95"><span class="nav-number">3.</span> <span class="nav-text">3. 变量分配测试</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">xudonglei</p>
  <div class="site-description" itemprop="description">搬砖 嵌入式</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">283</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">67</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xudonglei</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'BT0Qw9HcTpPv1a6cT1iDBXKz-gzGzoHsz',
      appKey     : 'LMCMh8v02JTMFAwKNRcgoizI',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
